package com.priox.sigar.client.dto;

import com.priox.sigar.util.StringUtil;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class RangoDetalleDTO {

    private String orden;
    private String expresion;
    private String descripcion;
    private BigDecimal tmpMinimo;
    private BigDecimal tmpMaximo;
    private String tmpExpresion1;
    private String tmpExpresion2;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RangoDetalleDTO that = (RangoDetalleDTO) o;

        if (orden != null ? !orden.equals(that.orden) : that.orden != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return orden != null ? orden.hashCode() : 0;
    }

    public String getExpresion() {
        return expresion;
    }

    public void setExpresion(String expresion) {
        this.expresion = expresion;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getOrden() {
        return orden;
    }

    public void setOrden(String orden) {
        this.orden = orden;
    }

    public BigDecimal getTmpMinimo() {
        return tmpMinimo;
    }

    public void setTmpMinimo(BigDecimal tmpMinimo) {
        this.tmpMinimo = tmpMinimo;
    }

    public BigDecimal getTmpMaximo() {
        return tmpMaximo;
    }

    public void setTmpMaximo(BigDecimal tmpMaximo) {
        this.tmpMaximo = tmpMaximo;
    }

    public String getTmpExpresion1() {
        return tmpExpresion1;
    }

    public void setTmpExpresion1(String tmpExpresion1) {
        this.tmpExpresion1 = tmpExpresion1;
    }

    public String getTmpExpresion2() {
        return tmpExpresion2;
    }

    public void setTmpExpresion2(String tmpExpresion2) {
        this.tmpExpresion2 = tmpExpresion2;
    }

    /**
     * <f:selectItem itemLabel="mayor a" itemValue="1" />
     * <f:selectItem itemLabel="mayor e igual a " itemValue="2" />
     * <f:selectItem itemLabel="menor a " itemValue="3" />
     * <f:selectItem itemLabel="menor e igual a" itemValue="4" />*
     */
    public void calcularExpresion() {
        Map map = new HashMap();
        map.put("1", ">");
        map.put("2", ">=");
        map.put("3", "<");
        map.put("4", "<=");
        if(tmpMaximo.intValue()==0){
            expresion = "[ Total " + map.get(tmpExpresion1) + " " + tmpMinimo + " ]";
        }else{
            expresion = "[ Total " + map.get(tmpExpresion1) + " " + tmpMinimo + " ] - [ Total " + map.get(tmpExpresion2) + " " + tmpMaximo + " ]";
        }
    }
    public void readExpresion(String value) {
        Map map = new HashMap();
        map.put(">","1");
        map.put(">=","2");
        map.put("<","3");
        map.put("<=","4");

        String[] item = StringUtils.split(value, " ");
        if (item.length > 5) {
            setTmpExpresion1((String) map.get(item[2]));
            setTmpMinimo(new BigDecimal(item[3]));
            setTmpExpresion1((String) map.get(item[8]));
            setTmpMaximo(new BigDecimal(item[9]));
        } else  {
            setTmpExpresion1((String) map.get(item[2]));
            setTmpMinimo(new BigDecimal(item[3]));
        }
    }
}
